package com.weetop.akly.activity.meessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.weetop.akly.R;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.base.mvp.BaseObserver;
import com.weetop.akly.bean.MsgBean;
import com.weetop.akly.bean.SignRecordBean;
import com.weetop.akly.presenter.SpRecordPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.TimeUtils;
import com.weetop.akly.view.SpRecordView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShenpiListActivity extends BaseActivity<SpRecordPresent> implements SpRecordView {

    @BindView(R.id.tv_tl_title)
    TextView tvTlTitle;
    @BindView(R.id.tv_tl_right)
    TextView tvTlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rcy_record)
    RecyclerView rcyRecord;
    @BindView(R.id.srl_sp_record)
    SmartRefreshLayout srlSpRecord;
    private ArrayList<MsgBean> data = new ArrayList<>();
    private boolean isRefresh, isLoadMore;
    private int page = 1;
//    private String keywords = "";

    @Override
    protected SpRecordPresent createPresenter() {
        return new SpRecordPresent(this, this);
    }

    private CommonRecyclerAdapter<MsgBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shenpi);
        ButterKnife.bind(this);
        setToolBar("审批", "");
        intListView();
        mPresenter.getSpRecord(page + "");
    }

    private void intListView() {
        rcyRecord.setItemAnimator(null);
        rcyRecord.setLayoutManager(new LinearLayoutManager(mActivity));
        srlSpRecord.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                mPresenter.getSpRecord(page + "");
                System.out.println("1111111111111111111111111");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                mPresenter.getSpRecord(page + "");
                System.out.println("222222222222222222222222222");
            }
        });
    }

    @Override
    public void getSpRecordSuccess(BaseModel<SignRecordBean> o) {
        List<MsgBean> items = o.getData().getList_data();
        showDataInList(items);
    }

    private void showDataInList(List<MsgBean> items) {
        if (adapter == null) {
            data.clear();
            data.addAll(items);
            adapter = new CommonRecyclerAdapter<MsgBean>(mActivity, R.layout.item_list_shenpi, data) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, MsgBean item, int position) {
                    //单据状态：-3删除-2撤销-1拒绝0未审核1审核中999审核通过
//                    int statusRes = R.mipmap.icon_error;
                    String statusStr = "";
                    // success,error,info,warning
//                    switch (item.getIcon()) {
//                        case "success":
//                            statusRes = R.mipmap.icon_sign_success;
//                            break;
//                        case "info":
//                        case "warning":
//                            statusRes = R.mipmap.icon_doing;
//                            break;
//                        case "error":
//                        default:
//                            statusRes = R.mipmap.icon_error;
//                            break;
//                    }
                    helper.setText(R.id.tv_time, BaseUtils.formatTime(item.getCreate_time()));

                    if (item.getStatus() == 0) {
                        statusStr = "未读";
                    } else {
                        statusStr = "已读";
                    }
                    helper.setText(R.id.tv_result, item.getTitle())
                            .setText(R.id.tv_status, statusStr)
                            .setText(R.id.tv_create_time, TimeUtils.fromLongToDate("yyyy-MM-dd", item.getCreate_time() * 1000))
                            .setText(R.id.tv_reason, item.getDetails());
                }
            };
            rcyRecord.setAdapter(adapter);
            adapter.setOnItemClickListener((viewHolder, view, position) -> {
                if (BaseUtils.fastClick()) {
                    read(data.get(position).getMsg_id() + "");
                }
            });
        } else {
            if (isRefresh) {
                data.clear();
                data.addAll(items);
                srlSpRecord.finishRefresh(true);
                isRefresh = false;
                adapter.replaceAll(data);
            } else if (isLoadMore) {
                data.addAll(items);
                adapter.addAll(items);
                isLoadMore = false;
            }
        }
        if (items == null || items.size() < 10) {
            srlSpRecord.finishLoadMoreWithNoMoreData();
        } else {
            srlSpRecord.finishLoadMore(true);
        }
    }

    private void read(String id) {
        addDisposable(apiServer.getMessageRead("1", id), new BaseObserver<BaseModel<Object>>(this) {
            @Override
            public void onSuccess(BaseModel<Object> o) {
                isRefresh = true;
                page = 1;
                mPresenter.getSpRecord(page + "");
            }

            @Override
            public void onError(String msg) {
                showError(msg);
            }
        });
    }
}